package com.baidu.controller;

import com.baidu.pojo.CustomerBean;
import com.baidu.service.CustomerService;
import com.baidu.utils.EmailSender;
import com.baidu.utils.MessSend;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 创作时间：2019/10/28 13:52
 * 作者：李增强
 */
@Controller
public class CustomerController {

    @Resource
    CustomerService customerService;

    @RequestMapping("/updateFindPwd")
    public void updateFindPwd(String cname,String pwd,HttpServletResponse response){
        customerService.updatePwd(cname,pwd);
        String rs = "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "恭喜您密码修改成功"+
                "</body>\n" +
                "</html>";
        try {
            response.getWriter().write(rs);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/toFindPwdUpdate")
    public String toFindPwdUpdate(String cname,String ycode,HttpServletRequest request,Model model){
        String tel = customerService.getCustomerTel(cname);
        String rscode = request.getSession().getAttribute(tel).toString();
        if(rscode.equals(ycode)){
            model.addAttribute("cname", cname);
            return "pwd_update";
        }else{
            tel = tel.substring(0, 4)+"*******";
            model.addAttribute("cname", cname);
            model.addAttribute("tel", tel);
            model.addAttribute("msg", "验证码输入错误");
            return "pwd_tel";
        }
    }


    @RequestMapping("/getFindPwdCode")
    @ResponseBody
    public void getFindPwdCode(String tel,String cname,HttpServletResponse response,HttpServletRequest request){

        String rstel = customerService.getCustomerTel(cname);
        if(rstel.equals(tel)){
            Double random = Math.random();
            String code = random.toString().substring(2, 8);
            request.getSession().setAttribute(tel,code);
            String info = MessSend.sendInfo(tel, code);
            try {
                response.getWriter().write(code);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            try {
                response.getWriter().write("手机号码有误");
            }catch (Exception e){
            }
        }
    }

    @RequestMapping("/toFindPwdTel")
    public String toFindPwdTel(String cname, Model model){
        String tel = customerService.getCustomerTel(cname);
        if(tel==null||"".equals(tel)){
            model.addAttribute("msg", "账户名称不存在");
            return "pwd_find";
        }else{
            tel = tel.substring(0, 4)+"*******";
            model.addAttribute("cname", cname);
            model.addAttribute("tel", tel);
            return "pwd_tel";
        }

    }

    @RequestMapping("/toFindPwd")
    public String toFindPwd(){
        return "pwd_find";
    }


    @RequestMapping("/toRegCus")
    public String toRegCus(){
        return "cus_reg";
    }

    /**
     *邮件激活
     * url:http://192.168.1.109:8888/cloudmanage/cdkEmail.do?id=123&code=saaaa
     */
    @RequestMapping("/cdkEmail")
    @ResponseBody
    public void cdkEmail(Integer id,String accode,HttpServletResponse response){
        /**
         * 拿着id去数据库把code查出来，比对正确了把用户状态更改一下就OK啦。
         */
        String msg = customerService.updateCustomerStatus(id,accode);
        response.setCharacterEncoding("UTF-8");
        String rs = "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body>\n" +
                msg+
                "</body>\n" +
                "</html>";
        try {
            response.getWriter().write(rs);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/cusReg")
    public String cusReg(CustomerBean customerBean,String ycode,HttpServletRequest request){
        if(customerBean!=null&&customerBean.getTel()!=null){
            String  scode = request.getSession().getAttribute(customerBean.getTel()).toString();
            CustomerBean cb = customerService.saveCusReg(customerBean, ycode, scode);
            if(cb!=null){
                EmailSender es = new EmailSender();
                Integer id=customerBean.getId();
                String info="http://192.168.1.109:8888/cloudmanage/cdkEmail.do?id="+id+"&accode="+customerBean.getAccode();
                es.emailSend(customerBean.getCemail(),info);
            }else{
                System.out.println("注册失败");
            }
        }

        return "";
    }

    @RequestMapping("/getCode")
    @ResponseBody
    public void getCode(String tel, HttpServletResponse response, HttpServletRequest request){
        Double random = Math.random();
        String code = random.toString().substring(2, 8);
        request.getSession().setAttribute(tel,code);
        String info = MessSend.sendInfo(tel, code);

            try {
                if(info!=null){
                    response.getWriter().write(code);
                }else{
                    response.getWriter().write("no");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }


    }
}
